package com.iqiyi.pps.epg.api.utils;

import com.alibaba.fastjson.JSONObject;
import com.iqiyi.pps.epg.api.utils.puyu.HttpSignatureUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by karkarrotxu on 2017/8/9.
 */
public final class ApiAlertPlatFormUtil {
    private static Logger logger = LoggerFactory.getLogger(ApiAlertPlatFormUtil.class);
    public static final int SEND_TYPE_CHAT = 1;
    public static final int SEND_TYPE_MAIL = 2;
    public static final int SEND_TYPE_MSG = 4;
    public static final int TOPIC_ID_LIVE = 1167;
    public static final String SECRET_KEY_EPG = "0582beb0-7760-4887-937e-1e7ca7237fa1";
    public static final String PLATFORM_URL = "http://alert.qiyi.domain/event/create/";

    private ApiAlertPlatFormUtil() {
    }

    public static void doSend(int topic_id, String secret_key, String data, String users, int sendType) {
        int hotchat_enabled = ((sendType & SEND_TYPE_CHAT) == SEND_TYPE_CHAT) ? 1 : 0;
        int email_enabled = ((sendType & SEND_TYPE_MAIL) == SEND_TYPE_MAIL) ? 1 : 0;
        int sms_enabled = ((sendType & SEND_TYPE_MSG) == SEND_TYPE_MSG) ? 1 : 0;
        Map<String, Object> params = new HashMap<>();
        params.put("topic_id", topic_id);
        params.put("secret_key", secret_key);
        params.put("data", data);
        params.put("sms_enabled", sms_enabled);
        params.put("email_enabled", email_enabled);
        params.put("hotchat_enabled", hotchat_enabled);
        params.put("users", users);
        String retValue = ApiHttpRequestUtil.originalPost(PLATFORM_URL, params, null);
        logger.info(params + "|" + retValue);
    }

    public static void doSendDefault(String sendData, String users, int sendType) {
        if (sendType < 1) {
            return;
        }
        doSend(TOPIC_ID_LIVE, SECRET_KEY_EPG, sendData, users, sendType);
    }

    public static String authList(String name, String currentPage) throws Exception{
        String key ="317A4902FD3775103246E61CE6AE54AE3425B6323440FD32";
        String clientId = "7";
        Map<String, Object> pp = new HashMap();
        pp.put("cprVideoName", URLEncoder.encode(name,"utf-8"));
        pp.put("currentPage", currentPage);
        pp.put("pageSize",10);
        //pp.put("showAll",0);
        Long ts = System.currentTimeMillis();
        pp.put("ts",ts);
        String auth = HttpSignatureUtil.createToken(clientId, key, pp);
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("CPR-Authentication",auth);
        Map<String, String> req = new HashMap<String, String>();
        for (Map.Entry<String, Object> entry : pp.entrySet()){
            req.put(entry.getKey(), entry.getValue().toString());
        }
        String query = HttpSignatureUtil.join(pp);
        String url = "http://test.cpr.qiyi.domain/cprInter/findAllCprListV2.action?" +query;
        String res = ApiHttpRequestUtil.getMethod(url, headers, req);
        return res;
    }

    public static void main(String[] args) throws Exception{
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("content", "报警测试");
//        doSend(TOPIC_ID_LIVE, SECRET_KEY_EPG, jsonObject.toJSONString(), "karkarrotxu", SEND_TYPE_CHAT);
        logger.info(authList("大家","1"));
    }
}
